// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var _css = require("./bankInfoList.css");
	var router = require("../hash/pageRouter");
	var tools = require("tools");
	var databind = require("databind");
	var html_bankInfo_create = require("./html_bankInfo_create.html");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		loadBlankInfoList();

		initEvent();
	};
	var initEvent = function() {
		$("#bankInfoList .blue_search_btn").unbind("click").click(function(){
			loadBlankInfoList();
		});
		
		$("#bankInfoList .createBlankInfo").unbind("click").click(function(){
			$("#popdialog").html(html_bankInfo_create);
			dialog.showDialog($("#create_bankInfo_dialog"));
			$("#create_bankInfo_dialog .dialogHandlerBtn").unbind("click").click(function(){
				databind.submitDom($("#create_bankInfo_dialog"), function(){
					base.alert(1, "创建成功！");
					dialog.hideDialog($("#create_bankInfo_dialog"));
					router.route("bankInfoManage/bankInfoList.html");
				});
			});
		});
	};
	
	var loadBlankInfoList = function() {
		//搜索条件初始化
		var oData = {
			accountBank: $("#bankInfoList input[name=accountBank]").val(),
			accountBankCard: $("#bankInfoList input[name=accountBankCard]").val(),
			accountBnakName: $("#bankInfoList input[name=accountBnakName]").val(),
			pageIndex: pageIndex,
			pageSize: pageSize
		};
		tools.loadList("inquireBlankInfoPagelist.json", $("#bankInfoList .table"), $("#bankInfoListTpl"), oData, function(){
			//列表详情
			$("#bankInfoList .detail").unbind("click").click(function(){
				var $this = $(this);
				router.route("bankInfoManage/bankInfoDetail.html?accountInfoId=" + $this.parents("tr").attr("accountInfoId"));
			});
			
			//置顶
			$("#bankInfoList .top").unbind("click").click(function(){
				var $this = $(this);
				topBlankInfo($this.parents("tr").attr("accountInfoId"));
			});
			
			//取消置顶
			$("#bankInfoList .cancelTop").unbind("click").click(function(){
				var $this = $(this);
				cancelTopBlankInfo($this.parents("tr").attr("accountInfoId"));
			});
			
			//列表编辑
			$("#bankInfoList .edit").unbind("click").click(function(){
				var $this = $(this);
				$("#popdialog").html(html_bankInfo_create);
				dialog.showDialog($("#create_bankInfo_dialog"));
				$("#create_bankInfo_dialog").attr("action", "updateBlankInfo");
				$("#create_bankInfo_dialog").find(".dialogTitle").html("编辑银行账户")
				$("#create_bankInfo_dialog").find("input[name=accountInfoId]").val($this.parents("tr").attr("accountInfoId"));
				databind.fillInput($("#create_bankInfo_dialog"), {
					action: "inquireBlankInfoDetail.json?accountInfoId=" + $this.parents("tr").attr("accountInfoId")
				});
				
				$("#create_bankInfo_dialog .dialogHandlerBtn").unbind("click").click(function(){
					databind.submitDom($("#create_bankInfo_dialog"), function(){
						base.alert(1, "编辑成功！");
						dialog.hideDialog($("#create_bankInfo_dialog"));
						router.route("bankInfoManage/bankInfoList.html");
					});
				});
			});
		});
	}
	
	//置顶
	var topBlankInfo = function(accountInfoId) {
		base.fnAjax({
			action: "topBlankInfo.json", //接口名称
			oData: {
				accountInfoId: accountInfoId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "置顶成功！");
					router.route("bankInfoManage/bankInfoList.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	//取消置顶
	var cancelTopBlankInfo = function(accountInfoId) {
		base.fnAjax({
			action: "cancelTopBlankInfo.json", //接口名称
			oData: {
				accountInfoId: accountInfoId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "取消置顶成功！");
					router.route("bankInfoManage/bankInfoList.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});